<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. Spring Boot ######1). 简介Spring Boot 是由 Pivotal 团队提供的全新框架，其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="分布式--Spring-Boot-微服务框架">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Spring-Boot-微服务框架/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. Spring Boot ######1). 简介Spring Boot 是由 Pivotal 团队提供的全新框架，其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9f1edbf427131be7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4f53219ee51032e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-88257983094df0ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-118a479585a50b25.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-34d60cbba8543832.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4fa7b101e0271eb6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5c77225141950e2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-ea62e2d9de8297a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-e0a921b369b10f27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-95c9736f08128484.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9068c5851a8e55dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8e9f8409446daf2b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0ec2c8a99202e03d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c8d4e3f4a1075713.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c2659ed963b613e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-3a7f823865c655af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-38f180480c3f5f35.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c77a7a7c0037b112.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-daba3b695b60f7b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-fd0b405be889aa49.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d7b3fb8c4a5f969e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f42b15ae58ad8e1c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5b7611469fe5d108.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-e3cea9e4bea74e91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-7b41b2a4921a82f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:07:28.019Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="分布式--Spring-Boot-微服务框架">
<meta name="twitter:description" content="####1. Spring Boot ######1). 简介Spring Boot 是由 Pivotal 团队提供的全新框架，其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-9f1edbf427131be7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Spring-Boot-微服务框架/"/>





  <title>分布式--Spring-Boot-微服务框架 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Distributed/分布式--Spring-Boot-微服务框架/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">分布式--Spring-Boot-微服务框架</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. <a href="https://projects.spring.io/" target="_blank" rel="noopener">Spring Boot</a></p>
<p>######1). 简介<br>Spring Boot 是由 Pivotal 团队提供的全新框架，其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。<br><a id="more"></a></p>
<p>######2). 好处<br>（1）它是Spring的升级版，Spring容器能做到的事情，它都能做到，而且更简便，从配置形式上来说，SpringBoot完全抛弃了繁琐的XML文件配置方式，而是替代性地用注解方式来实现，虽然本质来说，是差不多的（类似包扫描，注解扫描，类加载之类）。<br>（2）SpringBoot集成的插件更多，从而使用很多服务，都只是引入一个依赖，几个注解和Java类就可以用了，具体的参考相关手册。<br>（3）在Web应用开发这一块，之前的应用一般来说是打包成war包，再发布到相关服务器容器下（例如Tomcat），虽然SpringBoot也可以这么做，但在SpringBoot下更常见的形式是将SpringBoot应用打包成可执行jar包文件。之所以这么做，源于你可以直接将SpringBoot应用看成是一个Java Application，其Web应用可以没有webapp目录（更不用说web.xml了），它推荐使用html页面，并将其作为静态资源使用。 </p>
<p>######3). Spring Boot是什么<br>Spring Boot不是一个框架，它是一种用来轻松创建具有最小或零配置的独立应用程序的方式。这是方法用来开发基于Spring的应用，但只需非常少的配置。它提供了默认的代码和注释配置，快速启动新的Spring项目而不需要太多时间。它利用现有的Spring项目以及第三方项目来开发生产就绪(投入生产)的应用程序。它提供了一组pom或gradle构建文件，可以使用它们添加所需的依赖项，并且还便于自动配置。<br>Spring Boot根据其类路径上的库自动配置所需的类。假设应用程序想要与数据库交互，如果在类路径上有Spring数据库，那么它会自动建立与数据源类的连接。<br><img src="https://upload-images.jianshu.io/upload_images/3110861-9f1edbf427131be7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<p>######4). 主要目标</p>
<ul>
<li>为所有Spring开发提供一个基本的，更快，更广泛的入门体验。</li>
<li>开箱即用，但随着需求开始偏离默认值，快速启动。</li>
<li>提供大型项目(例如嵌入式服务器，安全性，度量，运行状况检查，外部化配置)常见的一系列非功能特性。</li>
<li>绝对没有代码生成以及不需要XML配置，完全避免XML配置。</li>
<li>为了避免定义更多的注释配置(它将一些现有的 Spring Framework 注释组合成一个简单的单一注释)</li>
<li>避免编写大量import语句。</li>
<li>提供一些默认值，以便在短时间内快速启动新项目。<br>######5). 新项目使用Spring Boot</li>
<li>简化基于Java的应用程序开发，单元测试和集成测试过程。</li>
<li>通过提供一些默认值来减少开发，单元测试和集成测试时间。</li>
<li>提高生产力。</li>
<li>当使用默认值时，Spring Boot有自己的看法。如果不指定详细信息，它将使用其自己的默认配置。如果想要持久化，但是没有在POM文件中指定任何东西，那么Spring Boot会- 将Hibernate带有HSQLDB数据库的配置作为JPA提供者。</li>
<li>为大型项目(例如嵌入式服务器，安全性，度量，健康检查，外部化配置)提供许多非常常见的非功能特性/解决方案。<br>######6). 优缺点<br><strong>Spring Boot的优点：</strong></li>
<li>使用Java或Groovy开发基于Spring的应用程序非常容易。</li>
<li>它减少了大量的开发时间并提高了生产力。</li>
<li>它避免了编写大量的样板代码，注释和XML配置。</li>
<li>Spring Boot应用程序与其Spring生态系统(如Spring JDBC，Spring ORM，Spring Data，Spring Security等)集成非常容易。</li>
<li>它遵循“自用默认配置”方法，以减少开发工作量。</li>
<li>它提供嵌入式HTTP服务器，如Tomcat，Jetty等，以开发和测试Web应用程序非常容易。</li>
<li>它提供CLI(命令行界面)工具从命令提示符，非常容易和快速地开发和测试Spring Boot(Java或Groovy)应用程序。</li>
<li>它提供了许多插件来开发和测试Spring启动应用程序非常容易使用构建工具，如Maven和Gradle。</li>
<li>它提供了许多插件，以便与嵌入式和内存数据库工作非常容易。</li>
</ul>
<p><strong>Spring Boot的限制:</strong></p>
<ul>
<li>将现有或传统的Spring Framework项目转换为Spring Boot应用程序是一个非常困难和耗时的过程。它仅适用于全新Spring项目。</li>
</ul>
<p>####2. Spring Boot</p>
<p>######1). 入门<br><strong>Spring Boot</strong>项目归根只是一个常规的Spring项目，只是利用了<strong>Spring Boot</strong>启动程序和自动配置。要创建<strong>Spring Boot</strong>应用程序的方法，Spring团队(The Pivotal Team)提供了以下三种方法。</p>
<ul>
<li>使用Spring Boot CLI工具</li>
<li>使用Spring STS IDE</li>
<li>使用<a href="http://start.spring.io/" target="_blank" rel="noopener">Spring Initializr</a></li>
</ul>
<p>可以使用Spring Boot开发两种基于Spring的应用程序：</p>
<ul>
<li>基于Java的应用程序</li>
<li>基于Groovy的应用程序</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-4f53219ee51032e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>######2). <a href="http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/" target="_blank" rel="noopener">Spring Boot CLI</a><br>I. 下载并解压<br><img src="https://upload-images.jianshu.io/upload_images/3110861-88257983094df0ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"><br>在Windows系统中命令提示符中设置Spring Boot CLI环境变量：<br>右键“我的电脑”-&gt;”高级系统设置”-&gt;”高级”-&gt;”环境变量”-&gt;选择”PATH”并新建并加上面的路径<code>D:\distribution\spring-2.0.2.RELEASE\bin</code>;<br><img src="https://upload-images.jianshu.io/upload_images/3110861-118a479585a50b25.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>验证：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">spring --version</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-34d60cbba8543832.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>II. spring 命令语法查看<code>spring --help</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-4fa7b101e0271eb6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>III. 创建HelloWorld.groovy文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">class HelloWorld &#123;</span><br><span class="line">	@RequestMapping(&quot;/&quot;)</span><br><span class="line">	String hello() &#123;</span><br><span class="line">		&quot;Hello mazaiting.&quot;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-5c77225141950e2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>IV. 运行<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">spring run HelloWorld.groovy</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-ea62e2d9de8297a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>V. 测试<br>在浏览器输入<code>http://localhost:8080/</code>即可访问<br><img src="https://upload-images.jianshu.io/upload_images/3110861-e0a921b369b10f27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>######3). Maven 安装Spring Boot<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Inherit defaults from Spring Boot --&gt;</span><br><span class="line">&lt;parent&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;2.0.2.RELEASE&lt;/version&gt;</span><br><span class="line">&lt;/parent&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Add typical dependencies for a web application --&gt;</span><br><span class="line">&lt;dependencies&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">&lt;/dependencies&gt;</span><br></pre></td></tr></table></figure></p>
<p>######4). Eclipse 使用 Spring Boot<br>I. 在pom.xml文件中添加SpringBoot的依赖<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;</span><br><span class="line">	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line"></span><br><span class="line">	&lt;groupId&gt;com.mazaiting&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;SpringBootTest&lt;/artifactId&gt;</span><br><span class="line">	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line">	&lt;packaging&gt;jar&lt;/packaging&gt;</span><br><span class="line"></span><br><span class="line">	&lt;name&gt;SpringBootTest&lt;/name&gt;</span><br><span class="line">	&lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line"></span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">		&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line">	&lt;!-- Spring-Boot --&gt;</span><br><span class="line">	&lt;parent&gt;</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;2.0.2.RELEASE&lt;/version&gt;</span><br><span class="line">	&lt;/parent&gt;</span><br><span class="line">	&lt;dependencies&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!-- Spring-Boot --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. 创建HelloWorld类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">@EnableAutoConfiguration</span><br><span class="line">public class HelloWorld &#123;</span><br><span class="line"></span><br><span class="line">	@RequestMapping(&quot;/&quot;)</span><br><span class="line">	String home() &#123;</span><br><span class="line">		return &quot;Hello mazaiting&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication.run(HelloWorld.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 执行结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-95c9736f08128484.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"><br>在浏览器输入<code>http://localhost:8080/</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-9068c5851a8e55dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p>IV. 注解解释</p>
<ul>
<li>@RestController: 构造型注释, 指定控制器，Spring会在处理传入的Web请求时考虑它.</li>
<li>@RequestMapping：注解提供了“路由”信息。它告诉Spring任何带有路径“<code>/</code>”的HTTP请求应该映射到<code>home</code>方法。<code>[@RestController](https://github.com/RestController &quot;@RestController&quot;)</code>注解告诉Spring将生成的字符串直接返回给调用者。</li>
<li>@EnableAutoConfiguration: 允许自动配置</li>
</ul>
<p>V. 创建可执行的jar包</p>
<ul>
<li><p>在pom.xml的project节点下添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;build&gt;</span><br><span class="line">	&lt;plugins&gt;</span><br><span class="line">		&lt;!-- 用于创建可执行jar --&gt;</span><br><span class="line">		&lt;plugin&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">		&lt;/plugin&gt;</span><br><span class="line">	&lt;/plugins&gt;</span><br><span class="line">&lt;/build&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>在项目的根目录下执行dos命令<code>mvn package</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-8e9f8409446daf2b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"><br><img src="https://upload-images.jianshu.io/upload_images/3110861-0ec2c8a99202e03d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图12.png"></p>
</li>
<li>执行完成后<code>\SpringBootTest\target\</code>目录下生成SpringBootTest-0.0.1-SNAPSHOT.jar文件<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c8d4e3f4a1075713.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图13.png"></li>
<li>执行<code>java -jar target/SpringBootTest-0.0.1-SNAPSHOT.jar</code>命令运行，并在浏览器输入<code>http://localhost:8080/</code>，同样可看到执行效果.</li>
</ul>
<p>######5). Spring Boot JSP 实例<br>I. 创建Maven Web项目<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c2659ed963b613e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图14.png"></p>
<p>II. 在pom文件中添加依赖<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; </span><br><span class="line">xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 </span><br><span class="line">	http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;</span><br><span class="line">	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line">	&lt;groupId&gt;com.mazaiting&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;SpringBoot-JSP&lt;/artifactId&gt;</span><br><span class="line">	&lt;packaging&gt;war&lt;/packaging&gt;</span><br><span class="line">	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line">	&lt;name&gt;SpringBoot-JSP Maven Webapp&lt;/name&gt;</span><br><span class="line">	&lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line">	&lt;!-- Spring-Boot --&gt;</span><br><span class="line">	&lt;parent&gt;</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;2.0.2.RELEASE&lt;/version&gt;</span><br><span class="line">	&lt;/parent&gt;</span><br><span class="line">	&lt;!-- 配置Java版本 --&gt;</span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">        &lt;java.version&gt;1.8&lt;/java.version&gt;</span><br><span class="line">    &lt;/properties&gt;</span><br><span class="line">	&lt;dependencies&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!-- Spring-Boot： This is a web application  --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!-- Tomcat embedded container--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;</span><br><span class="line">            &lt;scope&gt;provided&lt;/scope&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">        &lt;!-- Need this to compile JSP --&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;</span><br><span class="line">            &lt;scope&gt;provided&lt;/scope&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">         &lt;!-- JSTL for JSP --&gt;</span><br><span class="line">        &lt;!-- https://mvnrepository.com/artifact/javax.servlet/jstl --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">		    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;</span><br><span class="line">		    &lt;artifactId&gt;jstl&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;		</span><br><span class="line">		&lt;!-- https://mvnrepository.com/artifact/taglibs/standard --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">		    &lt;groupId&gt;taglibs&lt;/groupId&gt;</span><br><span class="line">		    &lt;artifactId&gt;standard&lt;/artifactId&gt;</span><br><span class="line">		    &lt;version&gt;1.1.2&lt;/version&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">        &lt;!-- Optional, test for static content, bootstrap CSS--&gt;</span><br><span class="line">        &lt;dependency&gt;</span><br><span class="line">            &lt;groupId&gt;org.webjars&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;bootstrap&lt;/artifactId&gt;</span><br><span class="line">            &lt;version&gt;3.3.7&lt;/version&gt;</span><br><span class="line">        &lt;/dependency&gt;</span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line"></span><br><span class="line">	&lt;build&gt;</span><br><span class="line">		&lt;finalName&gt;SpringBoot-JSP&lt;/finalName&gt;</span><br><span class="line">        &lt;plugins&gt;</span><br><span class="line">            &lt;!-- Package as an executable jar/war --&gt;</span><br><span class="line">			&lt;plugin&gt;</span><br><span class="line">				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">			&lt;/plugin&gt;</span><br><span class="line">        &lt;/plugins&gt;</span><br><span class="line">    &lt;/build&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></p>
<p>实现依赖关系：<code>mvn dependency:tree</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-3a7f823865c655af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图15.png"></p>
<p>III. SpringBootServletInitializer执行传统的WAR部署运行SpringApplication<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 注解为SpringBoot应用</span><br><span class="line">@SpringBootApplication</span><br><span class="line">public class SpringBootWebApplication extends SpringBootServletInitializer&#123;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) &#123;</span><br><span class="line">		return builder.sources(SpringBootWebApplication.class);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication.run(SpringBootWebApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 创建控制器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Controller</span><br><span class="line">public class WelcomeController &#123;</span><br><span class="line">	</span><br><span class="line">	// 注入应用配置 application.properties</span><br><span class="line">	@Value(&quot;$&#123;welcome.message&#125;&quot;)</span><br><span class="line">	private String message = &quot;Hello World&quot;;</span><br><span class="line">	</span><br><span class="line">	@RequestMapping(&quot;/&quot;)</span><br><span class="line">	public String welcome(Map&lt;String, Object&gt; model) &#123;</span><br><span class="line">		model.put(&quot;message&quot;, this.message);</span><br><span class="line">		return &quot;welcome&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V.  welcome.jsp页面<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">	pageEncoding=&quot;UTF-8&quot; isELIgnored=&quot;false&quot;%&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;spring&quot; uri=&quot;http://www.springframework.org/tags&quot;%&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;!-- 链接bootstrap --&gt;</span><br><span class="line">		&lt;!-- </span><br><span class="line">			href中的内容写为：webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">			加载地址为：http://localhost:8080/SpringBoot-JSP/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">			href中的内容写为：/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">			加载地址为：http://localhost:8080/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">			使用中建议写为webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">		--&gt;</span><br><span class="line">		&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;webjars/bootstrap/3.3.7/css/bootstrap.css&quot;/&gt;</span><br><span class="line">		&lt;%-- &lt;spring:url value=&quot;/css/main.css&quot; var=&quot;springCss&quot;/&gt;</span><br><span class="line">		&lt;link href=&quot;$&#123;springCss&#125;&quot; rel=&quot;stylesheet&quot;/&gt; --%&gt;</span><br><span class="line">		&lt;script type=&quot;text/javascript&quot; src=&quot;webjars/jquery/1.11.1/jquery.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">		&lt;c:url value=&quot;/css/main.css&quot; var=&quot;jstlCss&quot;&gt;&lt;/c:url&gt;</span><br><span class="line">	    &lt;link href=&quot;$&#123;jstlCss &#125;&quot; rel=&quot;stylesheet&quot;/&gt;</span><br><span class="line">		&lt;title&gt;SpringBootTest&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt; </span><br><span class="line">		&lt;nav class=&quot;navbar navbar-inverse&quot;&gt;</span><br><span class="line">			&lt;div class=&quot;container&quot;&gt;</span><br><span class="line">				&lt;div class=&quot;navbar-header&quot;&gt;</span><br><span class="line">					&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Spring Boot&lt;/a&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">			&lt;div id=&quot;navbar&quot; class=&quot;collapse navbar-collapse&quot;&gt;</span><br><span class="line">				&lt;ul class=&quot;nav nav-bar-nav&quot;&gt;</span><br><span class="line">					&lt;li class=&quot;active&quot;&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">					&lt;li&gt;&lt;a href=&quot;#about&quot;&gt;About&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">				&lt;/ul&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/nav&gt;</span><br><span class="line">		&lt;div class=&quot;container&quot;&gt;</span><br><span class="line">			&lt;div class=&quot;starter-template&quot;&gt;</span><br><span class="line">				&lt;h1&gt;Spring Boot Web JSP Example&lt;/h1&gt;</span><br><span class="line">				&lt;h2&gt;Message: $&#123;message &#125;&lt;/h2&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/div&gt;</span><br><span class="line">		&lt;script type=&quot;text/javascript&quot; src=&quot;webjars/bootstrap/3.3.7/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>注</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">href中的内容写为：webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">加载地址为：http://localhost:8080/SpringBoot-JSP/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line"></span><br><span class="line">href中的内容写为：/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line">加载地址为：http://localhost:8080/webjars/bootstrap/3.3.7/css/bootstrap.css</span><br><span class="line"></span><br><span class="line">使用中建议写为webjars/bootstrap/3.3.7/css/bootstrap.css</span><br></pre></td></tr></table></figure></p>
<p>main.css<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">h1 &#123;</span><br><span class="line">	color: #00F;</span><br><span class="line">&#125;</span><br><span class="line">h2 &#123;</span><br><span class="line">	color: #F00;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>VI. 在<code>/src/main/resources/</code>目录下创建application.properties及application-dev.properties、application-prod.properties、application-test.properties。</p>
<ul>
<li>application-dev.properties：用于开发环境</li>
<li>application-test.properties：用于测试环境</li>
<li>application-prod.properties：用于生产环境</li>
</ul>
<p>在application.properties文件中配置使用哪一类型的配置文件, prefix和suffix配置视图层的前后缀。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">spring.mvc.view.prefix: /jsp/</span><br><span class="line">spring.mvc.view.suffix: .jsp</span><br><span class="line">spring.profiles.active= dev</span><br></pre></td></tr></table></figure></p>
<p><strong>application-dev.properties</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">welcome.message: Hello mazaiting1</span><br></pre></td></tr></table></figure></p>
<p><strong>application-test.properties</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">welcome.message: Hello mazaiting2</span><br></pre></td></tr></table></figure></p>
<p><strong>application-prod.properties</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">welcome.message: Hello mazaiting3</span><br></pre></td></tr></table></figure></p>
<p>VII. 代码结构<br><img src="https://upload-images.jianshu.io/upload_images/3110861-38f180480c3f5f35.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图16.png"></p>
<p>VIII.  运行测试一：在SpringBootWebApplication类上右键Run As -&gt; Java Application, 在浏览器中输入<code>http://localhost:8080/</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-c77a7a7c0037b112.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图17.png"></p>
<p>IX..  运行测试二：在项目的根目录下，在命令提示符输入<code>mvn spring-boot:run</code>，然后在浏览器输入<code>http://localhost:8080/</code>也可呈现效果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-daba3b695b60f7b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图18.png"></p>
<p>X.  运行测试三：在工程右键-&gt; Run As -&gt; Run on Server部署在Tomcat容器中, 然后再浏览器中输入<code>http://localhost:8080/SpringBoot-JSP/</code>即可访问<br><img src="https://upload-images.jianshu.io/upload_images/3110861-fd0b405be889aa49.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图19.png"></p>
<p>XI. JSP限制<br>不能创建可执行jar来运行这个嵌入式Tomcat + JSP Web示例，因为Tomcat中有一个硬编码文件模式。</p>
<p>######6). Spring Boot创建war包，并部署在Tomcat<br>I. 使用命令提示符进入项目根目录<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn clean package</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-d7b3fb8c4a5f969e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图20.png"></p>
<p>II. 执行成功后，项目的<code>\SpringBoot-JSP\target\</code>目录下出现SpringBoot-JSP.war<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f42b15ae58ad8e1c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图21.png"></p>
<p>######7). Spring Boot + <a href="https://www.thymeleaf.org/" target="_blank" rel="noopener">Thymeleaf 模板引擎</a> –  可执行JAR文件<br>I. 引入依赖<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;</span><br><span class="line">	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line">	&lt;groupId&gt;com.mazaiting&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;SpringBoot-Thymeleaf&lt;/artifactId&gt;</span><br><span class="line">	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line">	&lt;name&gt;SpringBoot-Thymeleaf Maven Webapp&lt;/name&gt;</span><br><span class="line">	&lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line"></span><br><span class="line">	&lt;parent&gt;</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;1.4.2.RELEASE&lt;/version&gt;</span><br><span class="line">	&lt;/parent&gt;</span><br><span class="line">	&lt;!-- Java版本 --&gt;</span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">		&lt;java.version&gt;1.8&lt;/java.version&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line">	&lt;dependencies&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">		&lt;!-- hot swapping, disable cache for template, enable live reload --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;</span><br><span class="line">			&lt;optional&gt;true&lt;/optional&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">		&lt;!-- Optional, for bootstrap --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.webjars&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;bootstrap&lt;/artifactId&gt;</span><br><span class="line">			&lt;version&gt;3.3.7&lt;/version&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line">	&lt;build&gt;</span><br><span class="line">		&lt;finalName&gt;SpringBoot-Thymeleaf&lt;/finalName&gt;</span><br><span class="line">		&lt;plugins&gt;</span><br><span class="line">			&lt;!-- Package as an executable jar/war --&gt;</span><br><span class="line">			&lt;plugin&gt;</span><br><span class="line">				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">			&lt;/plugin&gt;</span><br><span class="line">		&lt;/plugins&gt;</span><br><span class="line">	&lt;/build&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>spring-boot-devtools有助于禁用缓存并启用热插拔，以便开发人员总是看到最后的更改。尝试修改Thymeleaf模板或属性文件，刷新浏览器以查看更改立即生效。</strong><br>II. 创建SpringBootWebApplication<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">public class SpringBootWebApplication &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication.run(SpringBootWebApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 创建WelcomeController<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Controller</span><br><span class="line">public class WelcomeController &#123;</span><br><span class="line">	@Value(&quot;$&#123;welcome.message&#125;&quot;)</span><br><span class="line">	private String message= &quot;Hello World&quot;;</span><br><span class="line">	</span><br><span class="line">	@RequestMapping(&quot;/&quot;)</span><br><span class="line">	public String welcome(Map&lt;String, Object&gt; model)&#123;</span><br><span class="line">		model.put(&quot;message&quot;, this.message);</span><br><span class="line">		return &quot;welcome&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. Thymeleaf +资源+静态文件</p>
<ul>
<li><p>welcome.html</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;/&gt;</span><br><span class="line">		&lt;title&gt;Spring Boot Thymeleaf Hello World 示例&lt;/title&gt;</span><br><span class="line">		&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;webjars/bootstrap/3.3.7/css/bootstrap.css&quot;/&gt;</span><br><span class="line">		&lt;link rel=&quot;stylesheet&quot; th:href=&quot;@&#123;/css/main.css&#125;&quot; href=&quot;../../css/main.css&quot;/&gt;</span><br><span class="line">		&lt;script type=&quot;text/javascript&quot; src=&quot;webjars/jquery/1.11.1/jquery.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;nav class=&quot;navbar navbar-inverse&quot;&gt;</span><br><span class="line">			&lt;div class=&quot;container&quot;&gt;</span><br><span class="line">				&lt;div class=&quot;navbar-header&quot;&gt;</span><br><span class="line">					&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Spring Boot&lt;/a&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">				&lt;div id=&quot;navbar&quot; class=&quot;collapse navbar-collapse&quot;&gt;</span><br><span class="line">					&lt;ul class=&quot;nav navbar-nav&quot;&gt;</span><br><span class="line">						&lt;li class=&quot;avtive&quot;&gt;&lt;a href=&quot;#&quot;&gt;首页&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">						&lt;li&gt;&lt;a href=&quot;#about&quot;&gt;关于&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">					&lt;/ul&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/nav&gt;</span><br><span class="line">		&lt;div class=&quot;container&quot;&gt;</span><br><span class="line">			&lt;div class=&quot;starter-template&quot;&gt;</span><br><span class="line">				&lt;h1&gt;Spring Boot Web Thymeleaf 示例&lt;/h1&gt;</span><br><span class="line">				&lt;h2&gt;</span><br><span class="line">					&lt;span th:text=&quot;&apos;Message: &apos; + $&#123;message&#125;&quot;&gt;&lt;/span&gt;</span><br><span class="line">				&lt;/h2&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/div&gt;</span><br><span class="line">		&lt;script type=&quot;text/javascript&quot; src=&quot;webjars/bootstrap/3.3.7/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>main.css</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">h1 &#123;</span><br><span class="line">	font-size: 20pt;</span><br><span class="line">&#125;</span><br><span class="line">h2 &#123;</span><br><span class="line">	font-size: 16pt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>application.properties</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">welcome.message: Hello, mazaiting</span><br></pre></td></tr></table></figure>
</li>
<li><p>目录结构<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5b7611469fe5d108.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图22.png"></p>
</li>
</ul>
<p>V. 运行方式一：运行SpringBootWebApplication，浏览器输入<code>http://localhost:8080/</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-e3cea9e4bea74e91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图23.png"></p>
<p>VI. 运行方式二：在命令提示符的项目根目录下执行<code>mvn spring-boot:run</code>，浏览器输入<code>http://localhost:8080/</code>即可显示效果<br>VII. 运行方式三：在命令提示符的项目根目录下执行<code>mvn clean package</code>，创建可执行jar包，运行jar包，浏览器输入<code>http://localhost:8080/</code>即可显示效果。想要生成jar包，在pom.xml文件中，一定有此行<code>&lt;packaging&gt;war&lt;/packaging&gt;</code></p>
<p>######8). Spring Boot 非Web应用–命令行应用<br>I. 创建Maven的普通Java工程，并在pom.xml文件中添加依赖<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;</span><br><span class="line">	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line"></span><br><span class="line">	&lt;groupId&gt;com.mazaiting&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;SpringBoot-Console&lt;/artifactId&gt;</span><br><span class="line">	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line">	&lt;packaging&gt;jar&lt;/packaging&gt;</span><br><span class="line"></span><br><span class="line">	&lt;name&gt;SpringBoot-Console&lt;/name&gt;</span><br><span class="line">	&lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line">	&lt;!-- Spring-Boot --&gt;</span><br><span class="line">	&lt;parent&gt;</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;2.0.2.RELEASE&lt;/version&gt;</span><br><span class="line">	&lt;/parent&gt;</span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">		&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line">	&lt;dependencies&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line"></span><br><span class="line">	&lt;build&gt;</span><br><span class="line">		&lt;plugins&gt;</span><br><span class="line">			&lt;!-- Package as an executable jar/war --&gt;</span><br><span class="line">			&lt;plugin&gt;</span><br><span class="line">				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">			&lt;/plugin&gt;</span><br><span class="line">		&lt;/plugins&gt;</span><br><span class="line">	&lt;/build&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. 创建HelloService服务<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">@Service</span><br><span class="line">public class HelloMessageService &#123;</span><br><span class="line">	</span><br><span class="line">	@Value(&quot;$&#123;name&#125;&quot;)</span><br><span class="line">	private String name;</span><br><span class="line"></span><br><span class="line">	public String getMessage() &#123;</span><br><span class="line">		return getMessage(name);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String getMessage(String name) &#123;</span><br><span class="line">		return &quot;Hello &quot; + name;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. application.properties内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">name=mazaiting</span><br></pre></td></tr></table></figure></p>
<p>IV. SpringBootConsoleApplication应用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">public class SpringBootConsoleApplication implements CommandLineRunner&#123;</span><br><span class="line"></span><br><span class="line">	@Autowired</span><br><span class="line">	private HelloMessageService helloService;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication application = new SpringApplication(SpringBootConsoleApplication.class);</span><br><span class="line">		application.setBannerMode(Banner.Mode.OFF);</span><br><span class="line">		application.run(args);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public void run(String... args) throws Exception &#123;</span><br><span class="line">		if (args.length &gt; 0) &#123;</span><br><span class="line">			System.out.println(helloService.getMessage(args[0].toString()));</span><br><span class="line">		&#125; else &#123;</span><br><span class="line">			System.out.println(helloService.getMessage());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 目录结构<br><img src="https://upload-images.jianshu.io/upload_images/3110861-7b41b2a4921a82f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图24.png"></p>
<p>VI. 运行</p>
<ul>
<li><p>生成jar包</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn package</span><br></pre></td></tr></table></figure>
</li>
<li><p>运行1</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar target/SpringBoot-Console-0.0.1-SNAPSHOT.jar</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>结果：<code>Hello mazaiting</code></p>
<ul>
<li>运行2<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar target/SpringBoot-Console-0.0.1-SNAPSHOT.jar spring-boot</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>结果：<code>Hello spring-boot</code></p>
<p>######9). Spring Boot 属性配置<br>I. pom.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;</span><br><span class="line">	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line"></span><br><span class="line">	&lt;groupId&gt;com.mazaiting&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;SpringBoot-Config&lt;/artifactId&gt;</span><br><span class="line">	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line">	&lt;packaging&gt;jar&lt;/packaging&gt;</span><br><span class="line"></span><br><span class="line">	&lt;name&gt;SpringBoot-Config&lt;/name&gt;</span><br><span class="line">	&lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line">	&lt;!-- Spring-Boot --&gt;</span><br><span class="line">	&lt;parent&gt;</span><br><span class="line">		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;2.0.2.RELEASE&lt;/version&gt;</span><br><span class="line">	&lt;/parent&gt;</span><br><span class="line"></span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">		&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line">	&lt;dependencies&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!-- Spring-Boot： This is a web application  --&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line"></span><br><span class="line">	&lt;build&gt;</span><br><span class="line">		&lt;plugins&gt;</span><br><span class="line">			&lt;!-- Package as an executable jar/war --&gt;</span><br><span class="line">			&lt;plugin&gt;</span><br><span class="line">				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">			&lt;/plugin&gt;</span><br><span class="line">		&lt;/plugins&gt;</span><br><span class="line">	&lt;/build&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. @Value使用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">@PropertySource(&quot;classpath:global.properties&quot;)</span><br><span class="line">public class GlobalProperties &#123;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;thread-pool&#125;&quot;)</span><br><span class="line">    private int threadPool;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;email&#125;&quot;)</span><br><span class="line">    private String email;</span><br><span class="line"></span><br><span class="line">    //getters and setters</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. ConfigurationProperties<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">@PropertySource(&quot;classpath:global.properties&quot;)</span><br><span class="line">@ConfigurationProperties</span><br><span class="line">public class GlobalProperties &#123;</span><br><span class="line"></span><br><span class="line">    private int threadPool;</span><br><span class="line">    private String email;</span><br><span class="line"></span><br><span class="line">    //getters and setters</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 配置文件<br>global.properties文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">email=zaitingma@foxmail.com</span><br><span class="line">thread-pool=12</span><br></pre></td></tr></table></figure></p>
<p>application.properties文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#Logging</span><br><span class="line">logging.level.org.springframework.web=ERROR</span><br><span class="line">logging.level.com.mazaiting=DEBUG</span><br><span class="line"></span><br><span class="line">#Global </span><br><span class="line">email=1425941077@qq.com</span><br><span class="line">thread-pool=10</span><br><span class="line"></span><br><span class="line">#App</span><br><span class="line">app.menus[0].title=Home</span><br><span class="line">app.menus[0].name=Home</span><br><span class="line">app.menus[0].path=/</span><br><span class="line">app.menus[1].title=Login</span><br><span class="line">app.menus[1].name=Login</span><br><span class="line">app.menus[1].path=/login</span><br><span class="line"></span><br><span class="line">app.compiler.timeout=5</span><br><span class="line">app.compiler.output-folder=/temp/</span><br><span class="line"></span><br><span class="line">app.error=/error/</span><br></pre></td></tr></table></figure></p>
<p><strong>注： 如果在application.properties文件中有与global.properties中有相同的属性，取值时则取application.properties中的配置</strong><br>V. 对应的java文件<br>AppProperties.java<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">//prefix app, find app.* values</span><br><span class="line">@ConfigurationProperties(&quot;app&quot;) </span><br><span class="line">public class AppProperties &#123;</span><br><span class="line">	private String error;</span><br><span class="line">	private List&lt;Menu&gt; menus = new ArrayList&lt;&gt;();</span><br><span class="line">	private Compiler compiler = new Compiler();</span><br><span class="line">	</span><br><span class="line">	public String getError() &#123;</span><br><span class="line">		return error;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setError(String error) &#123;</span><br><span class="line">		this.error = error;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public List&lt;Menu&gt; getMenus() &#123;</span><br><span class="line">		return menus;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setMenus(List&lt;Menu&gt; menus) &#123;</span><br><span class="line">		this.menus = menus;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Compiler getCompiler() &#123;</span><br><span class="line">		return compiler;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setCompiler(Compiler compiler) &#123;</span><br><span class="line">		this.compiler = compiler;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public static class Menu &#123;</span><br><span class="line">		private String name;</span><br><span class="line">		private String path;</span><br><span class="line">		private String title;</span><br><span class="line">		</span><br><span class="line">		public String getName() &#123;</span><br><span class="line">			return name;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		public void setName(String name) &#123;</span><br><span class="line">			this.name = name;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		public String getPath() &#123;</span><br><span class="line">			return path;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		public void setPath(String path) &#123;</span><br><span class="line">			this.path = path;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		public String getTitle() &#123;</span><br><span class="line">			return title;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		public void setTitle(String title) &#123;</span><br><span class="line">			this.title = title;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		@Override</span><br><span class="line">		public String toString() &#123;</span><br><span class="line">			return &quot;Menu [name=&quot; + name + &quot;, path=&quot; + path + &quot;, title=&quot; + title + &quot;]&quot;;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static class Compiler &#123;</span><br><span class="line">		private String timeout;</span><br><span class="line">		private String outputFolder;</span><br><span class="line">		public String getTimeout() &#123;</span><br><span class="line">			return timeout;</span><br><span class="line">		&#125;</span><br><span class="line">		public void setTimeout(String timeout) &#123;</span><br><span class="line">			this.timeout = timeout;</span><br><span class="line">		&#125;</span><br><span class="line">		public String getOutputFolder() &#123;</span><br><span class="line">			return outputFolder;</span><br><span class="line">		&#125;</span><br><span class="line">		public void setOutputFolder(String outputFolder) &#123;</span><br><span class="line">			this.outputFolder = outputFolder;</span><br><span class="line">		&#125;</span><br><span class="line">		@Override</span><br><span class="line">		public String toString() &#123;</span><br><span class="line">			return &quot;Compiler [timeout=&quot; + timeout + &quot;, outputFolder=&quot; + outputFolder + &quot;]&quot;;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;AppProperties [error=&quot; + error + &quot;, menus=&quot; + menus + &quot;, compiler=&quot; + compiler + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>GlobalProperties.java<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">@PropertySource(&quot;classpath:global.properties&quot;)</span><br><span class="line">public class GlobalProperties &#123;</span><br><span class="line">	@Value(&quot;$&#123;thread-pool&#125;&quot;)</span><br><span class="line">	private int threadPool;</span><br><span class="line">	@Value(&quot;$&#123;email&#125;&quot;)</span><br><span class="line">	private String email;</span><br><span class="line">	public int getThreadPool() &#123;</span><br><span class="line">		return threadPool;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setThreadPool(int threadPool) &#123;</span><br><span class="line">		this.threadPool = threadPool;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getEmail() &#123;</span><br><span class="line">		return email;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setEmail(String email) &#123;</span><br><span class="line">		this.email = email;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;GlobalProperties [threadPool=&quot; + threadPool + &quot;, email=&quot; + email + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>VI. 应用入口SpringBootWebApplication<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">public class SpringBootWebApplication &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication.run(SpringBootWebApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>VII. 控制器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">@Controller</span><br><span class="line">public class WelcomeController &#123;</span><br><span class="line">	private static final Logger LOGGER = LoggerFactory.getLogger(WelcomeController.class);</span><br><span class="line">	private GlobalProperties global;</span><br><span class="line">	private AppProperties app;</span><br><span class="line">	</span><br><span class="line">	@Autowired</span><br><span class="line">	public void setApp(AppProperties app) &#123;</span><br><span class="line">		this.app = app;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Autowired</span><br><span class="line">	public void setGlobalProperties(GlobalProperties global) &#123;</span><br><span class="line">		this.global = global;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@RequestMapping(&quot;/&quot;)</span><br><span class="line">	public String welcome(Map&lt;String, Object&gt; model) &#123;</span><br><span class="line">		String globalProperties = global.toString();</span><br><span class="line">		String appProperties = app.toString();</span><br><span class="line">		</span><br><span class="line">		LOGGER.debug(&quot;Welcome &#123;&#125;, &#123;&#125;&quot;, app, global);</span><br><span class="line">		</span><br><span class="line">		model.put(&quot;message&quot;, appProperties + globalProperties);</span><br><span class="line">		return &quot;welcome&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>VIII. 运行SpringBootWebApplication，浏览器输入<code>http://localhost:8080/</code>，查看控制台<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">2018-05-14 13:07:22.056 DEBUG 4228 --- [nio-8080-exec-3] com.mazaiting.config.WelcomeController   : </span><br><span class="line">Welcome AppProperties [error=/error/, menus=[Menu [name=Home, path=/, title=Home], </span><br><span class="line">Menu [name=Login, path=/login, title=Login]], compiler=Compiler [timeout=5, outputFolder=/temp/]],</span><br><span class="line"> GlobalProperties [threadPool=10, email=1425941077@qq.com]</span><br></pre></td></tr></table></figure></p>
<p>IX. 验证<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">@ConfigurationProperties</span><br><span class="line">public class GlobalProperties &#123;</span><br><span class="line"></span><br><span class="line">    @Max(5)</span><br><span class="line">    @Min(0)</span><br><span class="line">    private int threadPool;</span><br><span class="line"></span><br><span class="line">    @NotEmpty</span><br><span class="line">    private String email;</span><br><span class="line"></span><br><span class="line">    //getters and setters</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######10). 配置文件中配置logging<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#Logging</span><br><span class="line"># log level 日志等级</span><br><span class="line">logging.level.org.springframework.web=ERROR</span><br><span class="line">logging.level.com.mazaiting=DEBUG</span><br><span class="line"># log file 日志文件存放路径</span><br><span class="line">#logging.file=$&#123;java.io.tmpdir&#125;/application.log</span><br><span class="line"># output to a file 存放路径</span><br><span class="line">logging.file=C:/Users/Administrator/Desktop/mylog.log</span><br><span class="line"># pattern 格式化文件内容</span><br><span class="line">logging.pattern.file=&quot;%d&#123;yyyy-MM-dd HH:mm:ss&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n&quot;</span><br></pre></td></tr></table></figure></p>
<p>####<a href="https://gitee.com/mazaiting/distributed" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--Solr-站内搜索引擎/" rel="next" title="分布式--Solr-站内搜索引擎">
                <i class="fa fa-chevron-left"></i> 分布式--Solr-站内搜索引擎
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--Spring-Cloud/" rel="prev" title="分布式--Spring-Cloud">
                分布式--Spring-Cloud <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
